//
//  NearbyView.m
//  ITrip
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NearbyView.h"

@implementation NearbyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createMyView];
    }
    return self;
}

- (void)createMyView
{
    NSString *path = [URLPath strategyNearbyURL];
    self.arrayData = [NSMutableArray array];
    //下载
    //创建一个afn管理对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *params = nil;
    [manager GET:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.arrayData = [StrategyNearbyParser getNearbyParser:operation.responseData];
        [self initTableView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}

- (void)initTableView
{
    // 创建一个布局类
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(165, 160);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    self.myCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.frame.size.width, self.frame.size.height - 64) collectionViewLayout:layout];
    self.myCollect.delegate = self;
    self.myCollect.dataSource = self;
    
    //注册cell
    [self.myCollect registerClass:[StratrgyNearbyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.myCollect.backgroundColor = [UIColor colorWithRed:227/255.0 green:233/255.0 blue:240/255.0 alpha:1];
    
    [self addSubview:self.myCollect];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayData.count;
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StratrgyNearbyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    StrategyNearbyModel *model = [self.arrayData objectAtIndex:indexPath.row];
    [cell setStrategyNearbyModel:model];
    return cell;
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
